package com.dkd.manage.controller;

import com.dkd.common.core.domain.R;
import com.dkd.manage.pojo.dto.PartnerQueryListDto;
import com.dkd.manage.pojo.entity.Partner;
import com.dkd.manage.pojo.vo.PartnerVo;
import com.dkd.manage.result.PageResult;
import com.dkd.manage.service.PartnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@Api(tags = "合作商管理相关接口")
@RequestMapping("/manage/partner")
public class PartnerController {

    @Autowired
    private PartnerService partnerService;

    @ApiOperation("分页查询合作商列表")
    @PreAuthorize("@ss.hasPermi('manage:partner:list')")
    @GetMapping("/list")
    public R<PageResult<PartnerVo>> list(PartnerQueryListDto partnerQueryListDto) {
        log.info("分页查询合作商列表：{}", partnerQueryListDto);
        PageResult<PartnerVo> result = partnerService.list(partnerQueryListDto);
        return R.ok(result);
    }

    @ApiOperation("新增合作商")
    @PreAuthorize("@ss.hasPermi('manage:partner:add')")
    @PostMapping
    public R<Integer> add(@RequestBody Partner partner) {
        log.info("新增合作商：{}", partner);
        Integer row = partnerService.add(partner);
        return R.ok(row);
    }

    @ApiOperation("根据id查询合作商")
    @PreAuthorize("@ss.hasPermi('manage:partner:query')")
    @GetMapping("/{id}")
    public R<Partner> getPartnerById(@PathVariable Long id) {
        log.info("根据id查询合作商：{}", id);
        Partner partner = partnerService.getPartnerById(id);
        return R.ok(partner);
    }

    @ApiOperation("修改合作商")
    @PreAuthorize("@ss.hasPermi('manage:partner:edit')")
    @PutMapping
    public R<Integer> edit(@RequestBody Partner partner) {
        log.info("修改合作商：{}", partner);
        Integer row = partnerService.edit(partner);
        return R.ok(row);
    }

    @ApiOperation("根据id删除合作商")
    @PreAuthorize("@ss.hasPermi('manage:region:remove')")
    @DeleteMapping("/{ids}")
    public R<Integer> deletePartnerById(@PathVariable Long[] ids) {
        log.info("根据id删除合作商：{}", ids);
        Integer rows = partnerService.deletePartnerById(ids);
        return R.ok(rows);
    }


    @ApiOperation("重置合作商密码")
    @PreAuthorize("@ss.hasPermi('manage:partner:edit')")
    @PutMapping("/resetPwd/{id}")
    public R<Integer> resetPassword(@PathVariable Long id) {
        log.info("重置合作商密码：{}", id);
        Integer row = partnerService.resetPassword(id);
        return R.ok(row);
    }
}
